package com.thingsgrid.appmanager.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * 功能说明：文件处理类
 *
 * @author 李龙飞
 * @since 2019/8/19 17:34
 */
@Slf4j
public class FileProcess {

	private static final String FILE_PATH = "G:\\";

	/**
	 *说明：feign文件上传
	 *@author lilongfei
	 *@since 2019/9/2 16:33
	 */
	public static String feignFileUpload(String fileName,byte[] bytes){
		String tempFilePath = FILE_PATH+fileName;
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(FILE_PATH);
			if(!dir.exists()&& dir.isDirectory()){
				dir.mkdirs();
			}
			file = new File(tempFilePath);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return tempFilePath;
	}

	/**
	 * 说明：文件处理
	 *
	 * @author lilongfei
	 * @since 2019/8/19 17:34
	 */
	public static String fileUpload(MultipartFile file, String fileName) throws Exception {
		File createFile = new File(FILE_PATH);
		if (!createFile.exists()) {
			boolean mkdirResult = createFile.mkdir();
			if (!mkdirResult) {
				log.error("文件夹创建失败");
				return null;
			}
		}
		String filePath = createFile+ file.getOriginalFilename();
		File newFile = new File(filePath);
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));
		out.write(file.getBytes());
		out.flush();
		out.close();
		return filePath;
	}

	/**
	 *说明：切分字符串，并转成文件
	 *@author lilongfei
	 *@since 2019/8/23 8:57
	 */
	public static boolean splitDockerfileContent(String content) {
		Boolean bool = false;
		String filename = FILE_PATH + "Dockerfile";
		try {
			File file = new File(filename);
			if (!file.exists()) {
				boolean createState = file.createNewFile();
				if (!createState) {
					log.error("文件创建失败");
				}
				System.out.println(content+"//");
				bool = writeFileContent(filename, content);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bool;
	}


	private static boolean writeFileContent(String filePath, String fileStr) throws IOException {
		Boolean bool = false;
		String filein = fileStr + "\r\n";//新写入的行，换行
		String temp = "";
		FileInputStream fis = null;
		FileOutputStream fos = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		PrintWriter pw = null;
		try {
			File file = new File(filePath);
			//将文件输入输入流
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer buffer = new StringBuffer();
			//文件原有内容
			for (int i = 0; (temp = br.readLine()) != null; i++) {
				buffer.append(filein);
				buffer = buffer.append(System.getProperty("line.separator"));
			}
			buffer.append(filein);
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buffer.toString().toCharArray());
			pw.flush();
			bool = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			//不要忘记关闭
			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				fos.close();
			}
			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (fis != null) {
				fis.close();
			}
		}
		return bool;
	}

	/**
	 *说明：将byte数组转为文件
	 *@author lilongfei
	 *@since 2019/9/2 16:27
	 */
	private static File byte2file(byte[] bytes){
		String tempFileName = "news"+System.currentTimeMillis();
		String tempFilePath = "/tmp/"+tempFileName;
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File("/tmp/");
			if(!dir.exists()&& dir.isDirectory()){
				dir.mkdirs();
			}
			file = new File(tempFilePath);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}


}
